<?php
/**
 * Assists in validating arguments.
 * @author Alecsandru Gabriel DINU
 * @since Jun 27, 2008
 */
class Validate {
	
	/**
	 * Validate an argument, throwing IllegalArgumentException  
	 * if the argument is null.
	 * @param mixed The value to be check for nullity 
	 * @param string The error message to throw in case of null
	 * @access public
	 * @static 
	 * @throws  IllegalArgumentException 
	 */
	public static function notNull($mixed, $message = "") {
		 if ($mixed == null) {
		 	throw new IllegalArgumentException($message);
		 }
	}
	
	/**
	 * Checks if a string is empty. 
	 * Throws IllegalArgumentException if the argument String is empty (null or zero length).
	 * <br>
	 * - param=null throws IllegalArgumentException
	 * - param='' throws IllegalArgumentException
	 * - param=' ' throws IllegalArgumentException
	 * - param='aa' throws IllegalArgumentException
	 * <br>
	 * @param mixed The string to be checked 
	 * @param string The error message to throw in case of null
	 *  
	 * @access public
	 * @static 
	 * @throws  IllegalArgumentException
	 */
	public static function notEmpty($mixed, $message = "") {
		 if (StringUtils::isBlank($mixed)) {
		 	throw new IllegalArgumentException($message);
		 }
	}
	
	/**
	 * Checks if a e-mail is valid and throws IllegalArgumentException if not.
	 * @param string E-mail address to be checked
	 * @param string The error message to throw if the e-mail is invalid 
	 * @access public
	 * @static 
	 * @throws IllegalArgumentException
	 */
	public static function checkEmail($email, $message = "") {
		require_once 'lib/Zend/Validate/EmailAddress.php';
		
		$validator = new Zend_Validate_EmailAddress();
		if (!$validator->isValid($email)) {
		    // email is invalid; throw exception
		    $error_message = $message . "\n Cause:";
    		foreach ($validator->getMessages() as $message) {
    			$error_message .= "$message\n"; 
    		}
    		throw new IllegalArgumentException($error_message);
		}
	}
	
}